package com.dianrui.operation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.bean.Area;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChooseAreaListAdapter extends BaseAdapter {
    Context context;
     List<Area>list;
    AreaClickListener areaClickListener;

    public ChooseAreaListAdapter(Context context, List<Area> list) {
        this.context = context;
        this.list = list;
    }

    public void refresh(List<Area> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface AreaClickListener {
        void onAreaClick(String areaId,String areaName);

    }

    public void setOnBatteryClickListener(AreaClickListener areaClickListener) {
        this.areaClickListener = areaClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_choose_area, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (list.size()>0) {
            final Area area = list.get(position);
            holder.areaName.setText(area.area_name);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (areaClickListener != null) {
                        areaClickListener.onAreaClick(area.area_id,area.area_name);
                    }
                }
            });
        }
        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.area_name)
        TextView areaName;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
